﻿// See https://aka.ms/new-console-template for more information
using CSRedis;
using Newtonsoft.Json;
using QskjLibrary;
using System.Data;

Console.WriteLine("Hello, World!");
Task.Factory.StartNew(() =>
{
    try
    {
        CSRedisClient redisDb4 = null,redisDb=null;
        while (true)
        {
            if (redisDb4 == null)
            {
                redisDb4 = RedisHelp.GetInstance4();
                redisDb = RedisHelp.GetInstance();
            }
            GetOnlineData(redisDb4,redisDb);
            Thread.Sleep(1000);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("qskj直连设备在线数据刷新异常" + e);
    }
});
while (true)
{
    Thread.Sleep(5000);
}
void GetOnlineData(CSRedisClient redisDb4, CSRedisClient redisDb)
{
    List<string> strings = new List<string>();
    CSRedisClient? cSRedisClient6 = RedisHelp.GetInstance6();
    string onlineStr=cSRedisClient6.Get($@"refreshOnline");
    if (string.IsNullOrWhiteSpace(onlineStr))
    {
        string sql = $@"select car_numbers from qskj.B_TestRefreshOnline";
        DataTable dt = MySqlHelper.GetDataTable(CommandType.Text,sql);
        foreach (DataRow dr in dt.Rows)
        {
            strings.Add(dr["car_numbers"].ToString());
        }
        cSRedisClient6.Set($@"refreshOnline", JsonConvert.SerializeObject(strings), new TimeSpan(23, 59, 00));
    }
    else {
        strings = JsonConvert.DeserializeObject<List<string>>(onlineStr);
    }
    foreach (var item in strings)
    {
        redisDb4.Set(item, "online"); redisDb.Set(item, "0");
    } 
}